﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace testApp
{
    public partial class EmployeeForm : Form
    {
        DataManager dm = new DataManager();
        public int dbResultID;

        public EmployeeForm(int selEmpID, int depID, string actionType )
        {
            InitializeComponent();
            Employee emp = new Employee();
            this.cbxDepartment.DataSource = dm.GetDepartmentsAll();
            this.cbxDepartment.DisplayMember = "DepartmentName";
            this.cbxDepartment.ValueMember = "DepartmentID";
            this.cbxPost.DataSource = dm.GetAllPost();
            this.cbxPost.DisplayMember = "PostDesc";
            this.cbxPost.ValueMember = "PostID";

            switch (actionType)
            {
                case "add":
                    this.Text = "Add New Employee";
                    this.cbxDepartment.SelectedValue = depID;
                    this.tbxEmployeeID.Text = selEmpID.ToString();
                    break;
                case "show":
                    this.Text = "Details Employee";
                    emp = dm.GetEmployeeByID(selEmpID);
                    this.tbxEmployeeID.Text = selEmpID.ToString();
                    this.tbxName.Text = emp.Name;
                    this.tbxSecondName.Text = emp.SecondName;
                    this.tbxLastName.Text = emp.Surname;
                    this.tbxEmail.Text = emp.email;
                    this.tbxInfo.Text = emp.Info;
                    this.tbxMoney.Text = emp.Money.ToString();
                    this.tbxPhone.Text = emp.Phone;
                    this.dtpBirthdate.Value = emp.Birthdate;
                    this.dtpWorkdate.Value = emp.WorkDate;
                    this.cbxDepartment.SelectedValue = emp.DepartmentID;
                    this.cbxPost.SelectedValue = emp.PostID;
                    this.btnOK.Visible = false;
                    
                    this.tbxEmployeeID.Enabled = false;
                    this.tbxName.Enabled = false;

                    switch (emp.SexID)
                    {
                        case 1:
                            this.rbtnMale.Select();
                            break;
                        case 2:
                            this.rbtnFemale.Select();
                            break;
                        default:
                            break;

                    }
                    break;
                case "edit":
                    this.Text = "Edit Employee";
                    emp = dm.GetEmployeeByID(selEmpID);
                    this.tbxEmployeeID.Text = selEmpID.ToString();
                    this.tbxName.Text = emp.Name;
                    this.tbxSecondName.Text = emp.SecondName;
                    this.tbxLastName.Text = emp.Surname;
                    this.tbxEmail.Text = emp.email;
                    this.tbxInfo.Text = emp.Info;
                    this.tbxMoney.Text = emp.Money.ToString();
                    this.tbxPhone.Text = emp.Phone;
                    this.dtpBirthdate.Value = emp.Birthdate;
                    this.dtpWorkdate.Value = emp.WorkDate;
                    this.cbxDepartment.SelectedValue = emp.DepartmentID;
                    this.cbxPost.SelectedValue = emp.PostID;
                    switch (emp.SexID)
                    {
                        case 1:
                            this.rbtnMale.Select();
                            break;
                        case 2:
                            this.rbtnFemale.Select();
                            break;
                        default:
                            break;

                    }
                    break;
                default:
                    break;
            }
            
        }

        private string ValidateForm()
        {
            string result = "";
            if (this.tbxName.Text.Trim() == "")
            {
                result += "The name is empty! ";
            }
            if (this.tbxLastName.Text.Trim() == "")
            {
                result += "The last name is empty! ";
            }
            if (this.tbxEmail.Text.Trim() != "")
            {
                if (this.tbxEmail.Text.Contains('@') != true)
                {
                    result += "The email is empty or is not correct! ";
                }
            }
            if (this.dtpBirthdate.Value.ToString() == "")
            {
                result += "The birthdate is empty! ";
            }
            if (this.dtpWorkdate.Value.ToString() == "")
            {
                result += "The work date is empty! ";
            }
            if ((this.rbtnFemale.Checked) || (this.rbtnMale.Checked))
            {
            }
            else
            {
                result += "The sex is not chacked! ";
            }
            if (result == "")
            {
                result = "success";
            }
            return result;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            string result = ValidateForm();
            if (result == "success")
            {
                Employee newEmp = new Employee();
                newEmp.EmployeeID = int.Parse(this.tbxEmployeeID.Text.ToString());
                newEmp.Name = this.tbxName.Text.Trim();
                newEmp.SecondName = this.tbxSecondName.Text.Trim();
                newEmp.Surname = this.tbxLastName.Text.Trim();
                if (this.rbtnMale.Checked)
                {
                    newEmp.SexID = 1;
                }
                else
                {
                    newEmp.SexID = 2;
                }
                newEmp.Birthdate = this.dtpBirthdate.Value;
                newEmp.WorkDate = this.dtpWorkdate.Value;
                newEmp.PostID = int.Parse(this.cbxPost.SelectedValue.ToString());
                newEmp.DepartmentID = int.Parse(this.cbxDepartment.SelectedValue.ToString());
                newEmp.Info = this.tbxInfo.Text.Trim();
                newEmp.Phone = this.tbxPhone.Text.Trim();
                newEmp.email = this.tbxEmail.Text.Trim();
                newEmp.Money = Decimal.Parse(this.tbxMoney.Text.Trim());
                if (this.Text == "Add New Employee")
                {
                    dbResultID = dm.AddNewEmployee(newEmp);
                }
                else 
                {
                    dbResultID = dm.EditEmployee(newEmp);
                }
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                var res = MessageBox.Show(result);
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();

        }

    }
}
